﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using AncientEmpires_025_132.ResourceString;

namespace AncientEmpires_025_132
{
    public class Sound : InVisibleGameEntity
    {
        private List<SoundEffect> _effectMusic;
        public List<SoundEffect> EffectMusic
        {
            get { return _effectMusic; }
            set { _effectMusic = value; }
        }
        private List<SoundEffectInstance> _effectMusicInstance;
        public List<SoundEffectInstance> EffectMusicInstance
        {
            get { return _effectMusicInstance; }
            set { _effectMusicInstance = value; }
        }
        private int _iEffect;

        public int IEffect
        {
            get { return _iEffect; }
            set { _iEffect = value; }
        }
        private Song _songMusic;
        public Song SongMusic
        {
            get { return _songMusic; }
            set { _songMusic = value; }
        }
        private bool repeatSong;
        public bool RepeatSong
        {
            get { return repeatSong; }
            set { repeatSong = value; }
        }
        private bool repeatEffect;
        public bool RepeatEffect
        {
            get { return repeatEffect; }
            set { repeatEffect = value; }
        }
        private float myvolume;

        public float MyVolume
        {
            get { return myvolume; }
            set { myvolume = value; }
        }
        private float pitch;

        public float Pitch
        {
            get { return pitch; }
            set { pitch = value; }
        }
        private float pan;

        public float Pan
        {
            get { return pan; }
            set { pan = value; }
        }

        public Sound(SoundEffect effectMusic)
        {
            EffectMusicInstance = new List<SoundEffectInstance>();
            this.EffectMusic = new List<SoundEffect>();
            this.EffectMusic.Add(effectMusic);
            EffectMusicInstance[0] = EffectMusic[0].CreateInstance();
            IEffect = 0;
            MyVolume = 0.5f;
        }

        public Sound(Song songMusic)
        {
            this.EffectMusic = new List<SoundEffect>();
            this.SongMusic= songMusic;
            MyVolume = 0.5f;
            IEffect = 0;
        }
        public Sound(Song songMusic,SoundEffect effectMusic)
        {
            this.EffectMusic = new List<SoundEffect>();
            this.SongMusic = songMusic;
            this.EffectMusic.Add(effectMusic);
            EffectMusicInstance[0] = EffectMusic[0].CreateInstance();
            MyVolume = 0.5f;
            IEffect = 0;
        }
        public Sound(ContentManager content,string strSong, List<string> strEffect)
        {
            if (strSong != null)
            {
                SongMusic = content.Load<Song>(strSong);
            }
            if (strEffect.Count > 0)
            {
                EffectMusic = new List<SoundEffect>();
                EffectMusicInstance = new List<SoundEffectInstance>();
                for (int i = 0; i < strEffect.Count; i++)
                {
                    SoundEffect temp = content.Load<SoundEffect>(strEffect[i]);
                    this.EffectMusic.Add(temp);
                    this.EffectMusicInstance.Add(EffectMusic[EffectMusic.Count - 1].CreateInstance());
                }
            }
            MyVolume = 0.5f;
            IEffect = 0;
        }
        public void PlayEffect(float volume)
        {
            this.MyVolume = volume;
            for (int i = 0; i < EffectMusic.Count; i++)
            {
                if (IEffect == i)
                {
                    EffectMusicInstance[i].Volume = volume;
                    EffectMusicInstance[i].Play();
                }
            }
            
        }
        public void Stop()
        {
            
            for (int i = 0; i < EffectMusicInstance.Count; i++)
            {
                if(IEffect == i)
                EffectMusicInstance[i].Stop();
            }
            IEffect = 0;
        }
        public void PlaySong(float volume)
        {
            MyVolume = volume;
            MediaPlayer.Play(SongMusic);
            MediaPlayer.IsRepeating = RepeatSong;
            MediaPlayer.Volume = MyVolume;
        }
    }
}
